<?php

//require_once './thirdpary/Zend/Rest/Client.php';

class PayvmentService extends Zend_Rest_Client {

	private $currency = null;
	private $apiKey = null;
	private $payvmentID = null;

	public function __construct($apiKey, $payvmentID, $currency = 'EUR') {
                parent::__construct('https://api.payvment.com/rest');

                $this->apiKey = $apiKey;
                $this->payvmentID = $payvmentID;
                $this->currency = $currency;
	}

	public function addStoreItem($cartID,$itemCurrency,$itemID,$itemName,$itemPrice,$itemQty,$itemUrl,$itemShipping = array(),$itemOptions = array()) {
		$this->apiKey($this->apiKey);
		$this->payvmentID($this->payvmentID);
		$this->cartID($cartID);
		$this->currency($this->currency);

		$this->command('addStoreItem');

		// item specific parameters
		$this->itemCurrency($itemCurrency);
		$this->itemID($itemID);
		$this->itemName($itemName);
		$this->itemPrice($itemPrice);
		$this->itemQty($itemQty);
		$this->itemUrl($itemUrl);

		if ($itemShipping === false) {
			$this->itemNoShip(1);
		}
		else {
			if(count($itemShipping) > 1) {
				$this->itemShipping($itemShipping[0]);
				$this->itemShipping2($itemShipping[1]);
			}
			elseif (count($itemShipping) > 0) {
				$this->itemShipping($itemShipping[0]);
			}
		}

		if(count($itemOptions) > 3) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
			$this->itemOption3($itemOptions[2]);
			$this->itemOption4($itemOptions[3]);
		}
		elseif (count($itemOptions) > 2) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
			$this->itemOption3($itemOptions[2]);
		}
		elseif (count($itemOptions) > 1) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
		}
		elseif (count($itemOptions) > 0) {
			$this->itemOption1($itemOptions[0]);
		}

		return $this->get();
	}

	public function updateStoreItem($cartID,$itemID,$itemQty,$itemOptions = array(),$retailerID=false) {
		$this->apiKey($this->apiKey);

		if ($retailerID)
			$this->retailerID($retailerID);
		else
			$this->retailerID($this->payvmentID);

		$this->cartID($cartID);
		$this->currency($this->currency);

		$this->command('updateStoreItem');

		// item specific parameters
		$this->itemID($itemID);
		$this->itemQty($itemQty);

		if(count($itemOptions) > 3) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
			$this->itemOption3($itemOptions[2]);
			$this->itemOption4($itemOptions[3]);
		}
		elseif (count($itemOptions) > 2) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
			$this->itemOption3($itemOptions[2]);
		}
		elseif (count($itemOptions) > 1) {
			$this->itemOption1($itemOptions[0]);
			$this->itemOption2($itemOptions[1]);
		}
		elseif (count($itemOptions) > 0) {
			$this->itemOption1($itemOptions[0]);
		}

		return $this->get();
	}

	public function deleteStoreItem($cartID,$itemID,$itemOptions = array(),$retailerID=false) {
		$this->apiKey($this->apiKey);
		$this->cartID($cartID);
		$this->itemID($itemID);

		if ($retailerID)
			$this->retailerID($retailerID);
		else
			$this->retailerID($this->payvmentID);

		$this->command('deleteStoreItem');

		return $this->get();
	}

	public function createNewCartID() {
		$this->payvmentID($this->payvmentID);
		$this->apiKey($this->apiKey);
		$result = $this->get();
		return $result->CartID;
	}
}

//$client = new PayvmentClient();
//print_r($client->addStoreItem('ois5qp892k9uhiqkb4knfvvh82','EUR','test112','test product2',13.66,6,'http://localhost/test2'));
//print_r($client->updateStoreItem('ois5qp892k9uhiqkb4knfvvh82','test112',3));
//print_r($client->deleteStoreItem('ois5qp892k9uhiqkb4knfvvh82','test112'));
